<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html>
<html>
<head>
	<title>业务处理-采购申请-采购申请单</title>
	<meta charset="utf-8">
	<link rel="stylesheet" type="text/css" href="<%=basePath %>libs/bootstrap-3.3.7-dist/css/bootstrap.min.css">
	<link rel="stylesheet" type="text/css" href="<%=basePath %>libs/bootstrap-table-master/dist/bootstrap-table.min.css">
	<link rel="stylesheet" type="text/css" href="<%=basePath %>css/reset.css">
	<link rel="stylesheet" type="text/css" href="<%=basePath %>css/style.css">
	<link rel="stylesheet" type="text/css" href="<%=basePath %>css/iframe.css">
</head>
<body>
	<div id="mainContent">
		<div id="mainContent_content">
			<div class="page_header" >
			<a href="<%=basePath %>purchaseRequest/listPurchaseRequest.do">采购申请</a>
			<i class="icon-double-right"></i><a>采购申请单</a>
			</div>
			<div class="btn rightBtn">
				<c:if test="${pur.status=='0' && isEdit }">
				<input type="button" class="btn btn1"  value="新增物品" onclick="toAddPurchaseRequestItem();"></input>
				<input type="button" class="btn btn2" value="保存" onclick="saveEditPurchaseRequest();"></input>
				<input type="button" class="btn btn_danger" value="删除" onclick="confirmDeletePurchaseRequest(${pur.purchaseRequisitionID });"></input>
				<input type="button" class="btn btn2" value="提交" onclick="saveAndSubmit();"></input>
				</c:if>
				<c:if test="${isAdd }">
					<input type="button" class="btn btn1" value="复制" onclick="confirmCopyPurchaseRequest(${pur.purchaseRequisitionID });"></input>
				</c:if>
				<input type="button" class="btn btn2" value="下载PDF" onclick="window.open('<%=basePath %>purchaseRequest/download.do?purchaseRequisitionID=${pur.purchaseRequisitionID }');"></input>
				<input type="button" class="btn btn2" value="返回" onclick="location.href='<%=basePath %>purchaseRequest/listPurchaseRequest.do'"></input>
				
			</div>
			<form id="purchaseRequestDetailForm" class="form">
			<div class="box">
				<input type="hidden" name="purchaseRequisitionID" value="${pur.purchaseRequisitionID }"/>
				<p>
						<label>申请单号</label>
						<input type="text" class="form-control" readonly  value="${pur.applicationNo }"></input>
						<label>申请人</label>
						<input type="text" class="form-control" readonly value="${pur.applicantName }"></input>
						<label>部门</label>
						<select name="deptID" class="form-control select" <c:if test="${pur.status!='0' || !isEdit }">disabled</c:if>>
							<option value=""></option>
							<c:forEach items="${organizations }" var="org">
								<option value="${org.organizationid }" <c:if test="${pur.deptID==org.organizationid}">selected</c:if>>${org.organizationname }</option>
							</c:forEach>
						</select>
						<label>邮轮</label>
						<select name="cruiseID" class="form-control select" <c:if test="${pur.status!='0' || !isEdit }">disabled</c:if>>
							<option value=""></option>
							<c:forEach items="${cruiseList }" var="cruise">
								<option value="${cruise.cruiseID }" <c:if test="${pur.cruiseID==cruise.cruiseID}">selected</c:if>>${cruise.cruiseName }</option>
							</c:forEach>
						</select>
				</p>
				<p>
						<label>状态</label>
						<c:if test="${pur.status==0 }"><input class="form-control" readonly type="text" value="未提交"></c:if>
						<c:if test="${pur.status==1 }"><input class="form-control" readonly type="text" value="已提交"></c:if>
						<c:if test="${pur.status==2 }"><input class="form-control" readonly type="text" value="审核中"></c:if>
						<c:if test="${pur.status==3 }"><input class="form-control" readonly type="text" value="已通过"></c:if>
						<c:if test="${pur.status==4 }"><input class="form-control" readonly type="text" value="未通过"></c:if>
						<label>仓库</label>
						<select name="warehouseManagementID" class="form-control select" <c:if test="${pur.status!='0' || !isEdit }">disabled</c:if>>
							<c:forEach items="${warehouseList }" var="warehouse">
								<option value="${warehouse.warehousemanagementid }" <c:if test="${pur.warehouseManagementID==warehouse.warehousemanagementid}">selected</c:if>>${warehouse.warehousename }</option>
							</c:forEach>
						</select>
						<label>说明</label>
						<input type="text" class="form-control doubleWidth" name="explains" value="${pur.explains }" <c:if test="${pur.status!='0' || !isEdit }">readonly</c:if>></input>
				</p>
				<p>
						<label>备注</label>
						<input type="text" class="form-control maxWidth" name="remarks" value="${pur.remarks }"  <c:if test="${pur.status!='0' || !isEdit }">readonly</c:if>></input>
				</p>
			</div>
			<div class="tableBox box">
				<table class="table table-bordered table_text_center">
					<thead>
						<tr>
							<th>代码</th>
							<th>物品名称</th>
							<th>物品条码</th>
							<th>品牌</th>
							<th>单位</th>
							<th>库存</th>
							<th>采购数量</th>
							<th>小计</th>
							<c:if test="${pur.status=='0' && isEdit }">
							<th>操作</th>
							</c:if>
						</tr>
					</thead>
					<tbody id="purchaseRequestItemTbody">
					<c:forEach items="${itemList }" var="item">
						<tr>
							<td>${item.itemCode }</td>
							<td>${item.articleBarCode }</td>
							<td>${item.chineseName }</td>
							<td>${item.brand }</td>
							<td>${item.purchaseSpecifications}${item.salesUnit }/${item.purchasingUnit }</td>
							<td>${item.warehouseStock }(${item.realCurrentStock })</td>
							<c:if test="${pur.status=='0' && isEdit }">
							<td><input type='text' class="form-control" data-purchaseSpecifications="${item.purchaseSpecifications }" onkeyup='limitInputNumber(this);countItem(this);' onchange='limitInputNumber(this);countItem(this);' value='<fmt:formatNumber value="${item.number/item.purchaseSpecifications }" pattern="#0"/>'/></td>
							<td><input type='text' name='itemNumber' readonly value='${item.number }'/></td>
							<td><a class="btn btn-xs btn_danger" onclick='deleteItem(this);'>删除</a><input type='hidden' name='itemIds' value='${item.itemInformationID }' /></td>
							</c:if>
							<c:if test="${pur.status!='0' || !isEdit }">
							<td><fmt:formatNumber value="${item.number/item.purchaseSpecifications }" pattern="#0"/></td>
							<td>${item.number }</td>
							</c:if>
						</tr>
					</c:forEach>
					</tbody>
				</table>
<!-- 				<div class="btn">
					<input type="button" value="编辑"></input>
					<input type="button" value="导出模板"></input>
					<input type="button" value="导入物品"></input>
				</div> -->
			</div>
			<div class="box updateBox">
				<p>
					<label>创建时间</label>
					<input type="text" class="form-control" readonly value="<fmt:formatDate value='${pur.createDate }' type='date'/>"></input>
					<label>最后处理人</label>
					<input type="text" class="form-control" readonly value="${pur.lastPersonName }"></input>
					<label>最后处理时间</label>
					<input type="text" class="form-control" readonly value="<fmt:formatDate value='${pur.lastProcessingTime }' type='date'/>"></input>
				</p>
			</div>
			</form>
		</div>
		<jsp:include page="/jsp/basic/prompt.jsp"></jsp:include>
		<jsp:include page="/jsp/business/purchaseRequest/addPurchaseRequestItem.jsp"></jsp:include>
	</div>
	<script src="<%=basePath %>js/jquery-3.2.1.min.js"></script>
	<script src="<%=basePath %>libs/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
	<script src="<%=basePath %>libs/bootstrap-table-master/dist/bootstrap-table.js"></script>
	<script src="<%=basePath %>libs/bootstrap-table-master/dist/locale/bootstrap-table-zh-CN.min.js"></script>
	<script src="<%=basePath %>My97DatePickers/WdatePicker.js"></script>
	<script src="<%=basePath %>js/public.js"></script>
	<script type="text/javascript">
	function addToHasAddTable(item){
		var html="<tr>";
		html+="<td>"+item.itemCode+"</td>";
		html+="<td>"+item.chineseName+"</td>";
		html+="<td>"+item.articleBarCode+"</td>";
		html+="<td>"+item.brand+"</td>";
		html+="<td>"+item.warehouseStock+"("+item.realCurrentStock+")"+"</td>";
		html+="<td>"+item.purchaseSpecifications+item.salesUnit+"/"+item.purchasingUnit+"</td>";
		html+="<td><input type='text' class='form-control' data-purchaseSpecifications='"+item.purchaseSpecifications+"' onkeyup='limitInputNumber(this);countItem(this);' onchange='limitInputNumber(this);countItem(this);'/></td>";
		html+="<td><input type='text' class='form-control' name='itemNumber' value='0' readonly ></td>";
		html+="<td><a class='btn btn-xs btn_danger' onclick='deleteItem(this);'>删除</a>";
		html+="<input type='hidden' name='itemIds' value='"+item.itemInformationID+"' /></td>";
		html+="</tr>";
		$("#purchaseRequestItemTbody").append(html);
	}	
	
	function countItem(obj){
		var numInput=$(obj).parent().parent().find("input[name='itemNumber']");
		numInput.val("0");
		var num=$(obj).val()*$(obj).attr("data-purchaseSpecifications");
		numInput.val(num);
	}
	
	function deleteItem(obj){
		$(obj).parent().parent().remove();
	}
	
	function validateForm(){
		var length=$("#purchaseRequestItemTbody [name='itemIds']").length;
		if(!length>0){
			Prompt.alert("提示","物品种类数量必须大于0！")
			return false;
		}
		var arr=$("#purchaseRequestItemTbody [name='itemNumber']");
		for(var i=0;i<arr.length;i++){
			var num=$(arr[i]).val();
			if(num.trim()=='' || num<=0){
				Prompt.alert("提示","物品采购数量必须大于0！")
				return false;
			}
		}
		var deptID=$("#purchaseRequestDetailForm [name='deptID']").val();
		if(deptID==''){
			Prompt.alert("提示","请选择部门！")
			return false;
		}
		var cruiseID=$("#purchaseRequestDetailForm [name='cruiseID']").val();
		if(cruiseID==''){
			Prompt.alert("提示","请选择邮轮！")
			return false;
		}
		return true;
	}
	
	function saveEditPurchaseRequest(){
		if(!validateForm()) return ;
		$.ajax({
			url:"<%=basePath %>purchaseRequest/update.do",
			type:"post",
			async : false,
			dataType:"json",
			data : $("#purchaseRequestDetailForm").serialize(),
			success:function(data){
				if(data.result=="success"){
					Prompt.alert("提示","采购申请保存成功！",Prompt.refresh);
				}else{
					Prompt.alert("提示","采购申请保存失败！");
				}
			},
			error :function(){
				Prompt.alert("提示","采购申请保存失败！");
			}
		});
	}
	
	function saveAndSubmit(){
		if(!validateForm()) return ;
		$.ajax({
			url:"<%=basePath %>purchaseRequest/updateAndSubmit.do",
			type:"post",
			async : false,
			dataType:"json",
			data : $("#purchaseRequestDetailForm").serialize(),
			success:function(data){
				if(data.result=="success"){
					Prompt.alert("提示","采购申请已提交！",function(){
						location.href="<%=basePath %>purchaseRequest/listPurchaseRequest.do";
					});
				}else{
					Prompt.alert("提示","采购申请提交失败！");
				}
			},
			error :function(){
				Prompt.alert("提示","采购申请提交失败！");
			}
		});
	}
	
	function confirmCopyPurchaseRequest(id){
		var delFunc=function(){
			$.ajax({
				url:"<%=basePath %>purchaseRequest/copy.do",
				type:"post",
				async : false,
				dataType:"json",
				data : "purchaseRequestid="+id,
				success:function(data){
					if(data.result=="success"){
						Prompt.alert("提示","采购申请已成功复制！",function(){
							location.href="<%=basePath %>purchaseRequest/listPurchaseRequest.do";
						});
					}else{
						Prompt.alert("提示","采购申请复制失败！");
					}
				},
				error :function(){
					Prompt.alert("提示","服务器无响应，请刷新页面后重试！");
				}
			});
		}
		Prompt.confirm("提示","确认要复制该采购申请吗?",delFunc);
	}
	
	function confirmDeletePurchaseRequest(id){
		var delFunc=function(){
			$.ajax({
				url:"<%=basePath %>purchaseRequest/delete.do",
				type:"post",
				async : false,
				dataType:"json",
				data : "purchaseRequestid="+id,
				success:function(data){
					if(data.result=="success"){
						Prompt.alert("删除提示","采购申请已删除！",function(){
							location.href="<%=basePath %>purchaseRequest/listPurchaseRequest.do";
						});
					}else{
						Prompt.alert("提示","采购申请删除失败！");
					}
				},
				error :function(){
					Prompt.alert("删除提示","服务器无响应，请刷新页面后重试！");
				}
			});
		}
		Prompt.confirm("提示","确认要删除该采购申请吗?删除后无法恢复",delFunc);
	}
	</script>
</body>
</html>